package com.th.supcom.pds.config.client.page;

import java.util.List;

import net.carefx.framework.container.client.context.AppContext;
import net.carefx.framework.container.client.context.ContextValueCannotChangedException;
import net.carefx.framework.container.client.page.IPageEntryPoint;
import net.carefx.framework.container.client.page.PageContext;
import net.carefx.framework.reflection.shared.annotations.Reflectable;

import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.config.client.common.PdsConfigConstant;
import com.th.supcom.pds.config.client.common.PhamConfigServiceFactory;
import com.th.supcom.pds.config.client.model.PhamDispensaryConfigModel;
import com.th.supcom.pds.config.client.service.PhamDispensaryConfigRpcAsync;
import com.th.supcom.pds.config.client.widget.EditPhamDispensaryConfigWindow;

/**
 * PhamDispensaryConfig 入口类实现.
 * 
 * 药房属性.
 */
@Reflectable
public class PhamDispensaryMaintainEntryPoint implements IPageEntryPoint
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private static final PhamDispensaryConfigRpcAsync s_PHAM_DISPENSARY_CONFIG_ASYNC = PhamConfigServiceFactory.getPhamDispensaryConfigService ();

    @Override
    public void start (final PageContext context)
    {
        context.getWorkBench ().getBodyView ().getWorkspace ().getContextView ().asLayoutContainer ().hide ();
      
        try
        {
            AppContext.get ().register ("isGlobal", 0);
        }
        catch (ContextValueCannotChangedException e)
        {
            e.printStackTrace ();
        }
        PhamDispensaryConfigModel model=new PhamDispensaryConfigModel ();
        model.setPhamDispensaryCode (ClientUtils.getOrgModel ().getOrgCode ());
        s_PHAM_DISPENSARY_CONFIG_ASYNC.findPhamDispensaryConfigByExample (model,
                                                                          new AsyncCallback <List <PhamDispensaryConfigModel>> ()
                                                                          {
                                                                              @Override
                                                                              public void onFailure (final Throwable caught)
                                                                              {
                                                                                  MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                    caught.getMessage (),
                                                                                                    null);
                                                                              }

                                                                              @Override
                                                                              public void onSuccess (final List <PhamDispensaryConfigModel> result)
                                                                              {
                                                                                    //弹出窗口
                                                                                  PhamDispensaryConfigModel configModel=new PhamDispensaryConfigModel();
                                                                                  if(result.size ()!=0){
                                                                                      configModel=result.get (0) ;
                                                                                  }
                                                                                  LayoutContainer container=context.getContainer();
                                                                                  container.setWidth(980);
                                                                                  container.setHeight(540);
                                                                                  final EditPhamDispensaryConfigWindow editWindow = new EditPhamDispensaryConfigWindow (configModel);
                                                                                  editWindow.setHeading ("编辑药房属性");
                                                                                  FormPanel formPanel= editWindow.getAddPhamConfigForm();
                                                                                  Button button= editWindow.getAddButton();
                                                                                  formPanel.addButton(button);
                                                                                  formPanel.show();
                                                                                  container.add(formPanel);
                                                                                  /*editWindow.setWidth (980);
                                                                                  editWindow.setHeight (540);
                                                                                  editWindow.show ();*/
                                                                              }
                                                                          });
    }

    @Override
    public void stop (final PageContext context)
    {
        return;
    }
}